home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFskyedit / c / Interpolate < prev    next >
Encoding:
Text File  |  2003-10-16  |  6.6 KB  |  190 lines

  1. /*
  2.  *  SFskyedit - Star Fighter 3000 sky colours editor
  3.  *  Interpolation dbox
  4.  *  Copyright (C) 2001  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* ANSI library files */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. /* RISC OS library files */
  27. #include "wimp.h"
  28. #include "toolbox.h"
  29. #include "event.h"
  30. #include "wimplib.h"
  31. #include "window.h"
  32. #include "gadgets.h"
  33.  
  34. /* My library files */
  35. #include "err.h"
  36. #include "msgtrans.h"
  37. #include "Macros.h"
  38. #include "Pal256.h"
  39.  
  40. /* Local headers */
  41. #include "Utils.h"
  42. #include "EditSky.h"
  43. #include "Back-end.h"
  44. #include "Interpolate.h"
  45. #include "Main.h"
  46.  
  47. /* Gadgets */
  48. #define INTERPOLATE_CUSCOL1    0x06
  49. #define INTERPOLATE_CUSMENU1   0x07
  50. #define INTERPOLATE_CUSCOL2    0x09
  51. #define INTERPOLATE_CUSMENU2   0x0a
  52. #define INTERPOLATE_CANCEL     0x00
  53. #define INTERPOLATE_GO         0x01
  54.  
  55. ObjectId Interpolate_sharedid = NULL_ObjectId;
  56. static char start_col, end_col;
  57. static bool have_caret;
  58.  
  59. /* ----------------------------------------------------------------------- */
  60. /*                       Function prototypes                               */
  61.  
  62. static ToolboxEventHandler _Interpolate_buttonhandler, _Interpolate_colourshandler, _Interpolate_popuphandler, _Interpolate_openhandler;
  63.  
  64. /* ----------------------------------------------------------------------- */
  65. /*                         Public functions                                */
  66.  
  67. void Interpolate_initialise(ObjectId object)
  68. {
  69.   /* Record ID */
  70.   Interpolate_sharedid = object;
  71.  
  72.   /* Install handlers */
  73.   EF(event_register_toolbox_handler(object, ActionButton_Selected, _Interpolate_buttonhandler, NULL));
  74.   EF(event_register_toolbox_handler(pal256_sharedid, Pal256_ColourSelected, _Interpolate_colourshandler, NULL));
  75.   EF(event_register_toolbox_handler(object, PopUp_AboutToBeShown, _Interpolate_popuphandler, NULL));
  76.   EF(event_register_toolbox_handler(object, Window_AboutToBeShown, _Interpolate_openhandler, NULL));
  77.   EF(event_register_toolbox_handler(object, Window_HasBeenHidden, hand_back_caret, &have_caret));
  78.   EF(event_register_wimp_handler(object, -1, watch_caret, &have_caret));
  79.  
  80.   have_caret = false;
  81. }
  82.  
  83. /* ----------------------------------------------------------------------- */
  84. /*                         Private functions                               */
  85.  
  86. static int _Interpolate_openhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  87. {
  88.   /* Dialogue box about to open - set up from ancestor */
  89.   ViewData *view_data;
  90.   char validation[16];
  91.  
  92.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1);
  93.  
  94.   start_col = get_shade(&view_data->sky, view_data->selection_start);
  95.   sprintf(validation, "r2;C/%X", (palette[start_col] & 0xffffff00) >> 8);
  96.   RE(button_set_validation(0, id_block->self_id, INTERPOLATE_CUSCOL1, validation));
  97.  
  98.   end_col = get_shade(&view_data->sky, view_data->selection_end);
  99.   sprintf(validation, "r2;C/%X", (palette[end_col] & 0xffffff00) >> 8);
  100.   RE(button_set_validation(0, id_block->self_id, INTERPOLATE_CUSCOL2, validation));
  101.   return 1; /* claim event */
  102. }
  103.  
  104. /* ----------------------------------------------------------------------- */
  105.  
  106. static int _Interpolate_colourshandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  107. {
  108.   /* Colour selected by picker */
  109.   Pal256ColourSelectedEvent *pcse = (Pal256ColourSelectedEvent *)event;
  110.   ObjectId parent;
  111.   ComponentId parent_component, button;
  112.   char validation[16];
  113.  
  114.   E_RETV(toolbox_get_parent(0, id_block->self_id, &parent, &parent_component), 0);
  115.   if(parent != Interpolate_sharedid)
  116.     return 0; /* event not handled */
  117.  
  118.   /* Record new colour */
  119.   switch (parent_component) {
  120.     case INTERPOLATE_CUSMENU1:
  121.       start_col = pcse->colour_number;
  122.       button = INTERPOLATE_CUSCOL1;
  123.       break;
  124.     case INTERPOLATE_CUSMENU2:
  125.       end_col = pcse->colour_number;
  126.       button = INTERPOLATE_CUSCOL2;
  127.       break;
  128.     default:
  129.       return 0; /* eh?! */
  130.   }
  131.  
  132.   /* Display new colour */
  133.   sprintf(validation, "r2;C/%X", (palette[pcse->colour_number] & 0xffffff00) >> 8);
  134.   RE(button_set_validation(0, parent, button, validation));
  135.   return 1; /* claim event */
  136. }
  137.  
  138. /* ----------------------------------------------------------------------- */
  139.  
  140. static int _Interpolate_popuphandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  141. {
  142.   /* Colour picker about to pop up - set colour */
  143.   switch(id_block->self_component) {
  144.     case INTERPOLATE_CUSMENU1:
  145.       RE(Pal256_set_colour(((PopUpAboutToBeShownEvent *)event)->menu_id, start_col));
  146.       return 1; /* claim event */
  147.     case INTERPOLATE_CUSMENU2:
  148.       RE(Pal256_set_colour(((PopUpAboutToBeShownEvent *)event)->menu_id, end_col));
  149.       return 1; /* claim event */
  150.   }
  151.   return 0; /* event not handled */
  152. }
  153.  
  154. /* ----------------------------------------------------------------------- */
  155.  
  156. static int _Interpolate_buttonhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  157. {
  158.   ActionButtonSelectedEvent *abse = (ActionButtonSelectedEvent *)event;
  159.   ViewData *view_data;
  160.  
  161.   switch(id_block->self_component) {
  162.     case INTERPOLATE_GO:
  163.       E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1);
  164.  
  165.       /* Stupidity checks */
  166.       if(!view_data->selection_exists
  167.       || ((view_data->selection_end+1) - view_data->selection_start) < 2) {
  168.         _kernel_oswrch(7); /* warning beep */
  169.         return 1; /* claim event */
  170.       }
  171.       
  172.       /* Do the business */
  173.       gradient_overwrite(&view_data->sky, view_data->selection_start, view_data->selection_end, start_col, end_col, 3);
  174.       EditSky_rowsupdated(view_data, view_data->selection_start, view_data->selection_end);
  175.       EditSky_markaschanged(view_data);
  176.  
  177.       return 1; /* claim event */
  178.  
  179.     case INTERPOLATE_CANCEL:
  180.       if(FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust))
  181.         /* Reset dbox state */
  182.         _Interpolate_openhandler(Window_AboutToBeShown, NULL, id_block, handle);
  183.  
  184.       return 1; /* claim event */
  185.       
  186.     default:
  187.       return 0; /* unknown button */
  188.   }
  189. }
  190.